package com.c6906.vdam.dps.config;

import org.apache.shardingsphere.api.sharding.standard.PreciseShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue;

import java.util.Collection;

/**
 * @Author : TAOHUIQI
 * @Date: 2024/7/7 15:38
 * @Description:
 */
public class YearShardingAlgorithm implements PreciseShardingAlgorithm<String> {

    @Override
    public String doSharding(Collection<String> availableTargetNames, PreciseShardingValue<String> shardingValue) {
        // 假设分库的字段值是 "2023-01-01"
        String date = shardingValue.getValue();
        int year = Integer.parseInt(date.substring(0, 4));
        if (year < 2024){
            year = 2024;
        }
        // 返回对应年份的数据源名称
        return "vdam" + year;
    }

}
